package com.example.utils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.spring.PropertyPreFilters;
import org.apache.poi.ss.formula.functions.T;

import java.util.List;


public class JSONUtils {

    /**
     * SerializerFeature.WriteMapNullValue  包含值为null
     * SerializerFeature.PrettyFormat   默认忽视值不为null
     */

    /**
     * 排除掉不需要的字段
     * @param t
     * @param excludes
     * @param <T>
     * @return
     */
    public static <T> String getJSONandExclude(T t,String [] excludes){
        PropertyPreFilters filters = new PropertyPreFilters();
        PropertyPreFilters.MySimplePropertyPreFilter excludefilter = filters.addFilter();
        excludefilter.addExcludes(excludes);
        return JSONObject.toJSONString(t,excludefilter,SerializerFeature.PrettyFormat);
    }

    /**
     *
     * JSON 只保留需要部分字段
     * @param t
     * @param strings
     * @param <T>
     * @return
     */
    public static <T> String getJSONandIncludString(T t,String[] strings){
        PropertyPreFilters filters = new PropertyPreFilters();
        PropertyPreFilters.MySimplePropertyPreFilter includefilter = filters.addFilter();
        includefilter.addIncludes(strings);
        return JSONObject.toJSONString(t,includefilter,SerializerFeature.PrettyFormat);
    }


    /**
     * 忽略get + 时间处理，默认是yyyy-MM-dd HH:mm:ss
     * @param t
     * @param <T>
     * @return
     */
    public static <T> String getBeanValueString(T t){
        return JSONObject.toJSONString(t, SerializerFeature.IgnoreNonFieldGetter,SerializerFeature.WriteDateUseDateFormat);
    }

    /**
     * 时间统一处理 或可用 @JSONField(format = "yyyy-MM-dd")
     * @param t
     * @param formart
     * @param <T>
     * @return
     */
    public static <T> String getBeanValueDate(T t,String formart){
        return JSONObject.toJSONStringWithDateFormat(t,formart,SerializerFeature.WriteDateUseDateFormat);
    }

    public static <T> String formartDateList(List<T> ts){
        return JSONArray.toJSONString(ts,SerializerFeature.IgnoreNonFieldGetter,SerializerFeature.WriteDateUseDateFormat);
    }

    public static void main(String[] args) {
        JSONArray jsonArray=new JSONArray();
        for (int i = 0; i < 1000000; i++) {
          JSONObject jsonObject=new JSONObject();
          jsonObject.put("key",i);
          jsonArray.add(jsonObject);
        }

        System.out.println(1111111);
        for (int i = 0; i < 1000; i++) {
            Long start1=System.currentTimeMillis();
            Object o3=getBOByField(jsonArray,"key","100000");
           //Object o4=getBOByField2(jsonArray,"key","100000");
           //getBOByField3(jsonArray,"key","100000");
            Long end3=System.currentTimeMillis();
            System.out.println(i+"."+(end3-start1));
        }


    }


    public static JSONObject getBOByField3(JSONArray jsonArray,String key,String value){
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject=(JSONObject) jsonArray.get(i);
            if(jsonObject.get(key)!=null){
                String obj=jsonObject.getString(key);
                if(obj.equals(value)){
                    return jsonObject;
                }
            }
        }
        return new JSONObject();
    }



    public static JSONObject getBOByField(List<JSONObject> bos,String key,String value){
        return bos.parallelStream().filter(e->{
            if(e.get(key)!=null){
                String obj=e.getString(key);
                if(obj.equals(value)){
                    return true;
                }
            }
            return false;
        }).findFirst().orElse(new JSONObject());

    }

    public static Object getBOByField(JSONArray jsonArray,String key,String value){
        return jsonArray.parallelStream().filter(e->{
            JSONObject jsonObject = (JSONObject) e;
            if(jsonObject.get(key)!=null){
                String obj=jsonObject.getString(key);
                if(obj.equals(value)){
                    return true;
                }
            }
            return false;
        }).findFirst().orElse(new JSONObject());

    }

    public static Object getBOByField2(JSONArray jsonArray,String key,String value){
        return jsonArray.stream().filter(e->{
            JSONObject jsonObject = (JSONObject) e;
            if(jsonObject.get(key)!=null){
                String obj=jsonObject.getString(key);
                if(obj.equals(value)){
                    return true;
                }
            }
            return false;
        }).findFirst().orElse(new JSONObject());

    }

}
